package com.ald.crossex.system.wechat;

import com.ald.crossex.entity.TCeCoreSystem;
import com.ald.crossex.service.SystemService;
import com.ald.crossex.system.SystemCache;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by lishijiong on 15/3/24.
 */
public class InitDataListener implements InitializingBean, ServletContextAware {

    RefreshAccessTokenJob refreshAccessTokenJob;
    RefreshJsApiTicketJob refreshJsApiTicketJob;
    SystemService systemService;

    public RefreshJsApiTicketJob getRefreshJsApiTicketJob() {
        return refreshJsApiTicketJob;
    }

    public void setRefreshJsApiTicketJob(RefreshJsApiTicketJob refreshJsApiTicketJob) {
        this.refreshJsApiTicketJob = refreshJsApiTicketJob;
    }

    public RefreshAccessTokenJob getRefreshAccessTokenJob() {
        return refreshAccessTokenJob;
    }

    public void setRefreshAccessTokenJob(RefreshAccessTokenJob refreshAccessTokenJob) {
        this.refreshAccessTokenJob = refreshAccessTokenJob;
    }

    public SystemService getSystemService() {
        return systemService;
    }

    public void setSystemService(SystemService systemService) {
        this.systemService = systemService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        refreshAccessTokenJob.execute();
        refreshJsApiTicketJob.execute();
        systemService.cacheSystem();

    }

    @Override
    public void setServletContext(ServletContext servletContext) {

//        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//        refreshAccessTokenJob = (RefreshAccessTokenJob) applicationContext.getBean("refreshAccessTokenJob");
//        refreshJsApiTicketJob = (RefreshJsApiTicketJob) applicationContext.getBean("refreshJsApiTicketJob");

    }
}
